package com.ruoyi.myshop.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.myshop.domain.Home;
import com.ruoyi.myshop.pojo.vo.HomeVo;
import com.ruoyi.myshop.pojo.vo.SumByCategoryVo;
import com.ruoyi.myshop.pojo.vo.SumByMonthVo;
import com.ruoyi.myshop.service.IHomeService;
import com.ruoyi.myshop.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 首页管理Controller
 *
 * @author HuaQP
 * @date 2024-07-02
 */
@RestController
@RequestMapping("/myshop/home")
public class HomeController extends BaseController
{
    @Autowired
    private IHomeService homeService;

    @Resource
    private IOrderService orderService;

    /**
     * 查询首页管理列表
     */
    @PreAuthorize("@ss.hasPermi('myshop:home:list')")
    @GetMapping("/list")
    public TableDataInfo list(Home home)
    {
        startPage();
        List<HomeVo> list = homeService.selectHomeList(home);
        return getDataTable(list);
    }

    /**
     * 导出首页管理列表
     */
    @PreAuthorize("@ss.hasPermi('myshop:home:export')")
    @Log(title = "首页管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Home home)
    {
        List<HomeVo> list = homeService.selectHomeList(home);
        ExcelUtil<HomeVo> util = new ExcelUtil<>(HomeVo.class);
        util.exportExcel(response, list, "首页管理数据");
    }

    /**
     * 获取首页管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('myshop:home:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(homeService.selectHomeById(id));
    }

    /**
     * 新增首页管理
     */
    @PreAuthorize("@ss.hasPermi('myshop:home:add')")
    @Log(title = "首页管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Home home)
    {
        return toAjax(homeService.insertHome(home));
    }

    /**
     * 修改首页管理
     */
    @PreAuthorize("@ss.hasPermi('myshop:home:edit')")
    @Log(title = "首页管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Home home)
    {
        return toAjax(homeService.updateHome(home));
    }

    /**
     * 删除首页管理
     */
    @PreAuthorize("@ss.hasPermi('myshop:home:remove')")
    @Log(title = "首页管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(homeService.deleteHomeByIds(ids));
    }


    /**统计日期区间订单商品分类销售数据 GET
     *
     * dtBgn 开始日期（如:'20120101') dtEnd 结束日期（如:'20121231')
     */
    @GetMapping(value = "/queryOrderSumByCategory")
    public AjaxResult queryOrderSumByCategory(@RequestParam String dtBgn, @RequestParam String dtEnd) {
        // 返回列表数据 :分类名categoryName、数量count、金额amount
        List<SumByCategoryVo> voList = orderService.queryOrderSumByCategory(dtBgn, dtEnd);
        return AjaxResult.success(voList);
    }

    /**
     * 统计年度月份订单销售数据 GET
     *
     * year指定年份（如:2012)
     */
    @GetMapping(value = "/queryOrderSumByMonth")
    public AjaxResult queryOrderSumByMonth(@RequestParam Integer year) {
        // 返回列表数据 :月份month、数量count、金额amount
        List<SumByMonthVo> voList = orderService.queryOrderSumByMonth(year);
        return AjaxResult.success(voList);
    }
}
